package com.sd.activity;

import java.io.File;
import java.util.Vector;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

import com.alex.books.BaseBook;
import com.alex.books.BookStore;
import com.alex.database.BookDao;
import com.sd.view.BookAdapter;


/**
 * 
 * 
 * @author Alex.Lu
 *
 */
public class MainActivity extends Activity implements OnClickListener, OnItemClickListener, OnItemLongClickListener {
	
	private static final String TAG = "MainActivity";
	private static final int REQUEST_EX = 1;
	
	private Button mFindBT;
	private Button mAutoFindbookBT;
	private Button mCamScannerBT;
	private Button mEBookBT;
	private GridView mBooksGV;
	private BookAdapter mBookAdapter;
	private Vector<BaseBook> mBooks;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
        setContentView(R.layout.main_activity);
        
        mBooksGV = (GridView)findViewById(R.id.books);
        mBooks = BookStore.getInstance().getBooks(this);
        mBookAdapter = new BookAdapter(this, mBooks);
        
        mBooksGV.setAdapter(mBookAdapter);
        mBooksGV.setOnItemClickListener(this);
        mBooksGV.setOnItemLongClickListener(this);
        
        mEBookBT = (Button)findViewById(R.id.e_book_btn);
        mEBookBT.setOnClickListener(this);
        
        mFindBT = (Button)findViewById(R.id.findbook_btn);
        mFindBT.setOnClickListener(this);
        
        mAutoFindbookBT = (Button)findViewById(R.id.auto_findbook_btn);
        mAutoFindbookBT.setOnClickListener(this);
        
        mCamScannerBT = (Button)findViewById(R.id.cam_scan_btn);
        mCamScannerBT.setOnClickListener(this);
        
    }
    
    protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		if (resultCode == RESULT_OK) {
			if (requestCode == REQUEST_EX) {
				Uri uri = intent.getData();
				String url = uri.getPath();
				
				BaseBook book = new BaseBook();
				book.mBookName =url.substring(url.lastIndexOf("/")+1,url.lastIndexOf("."));
				book.mBookPath = url;
				
				for(int i=0;i<mBooks.size();i++){
					BaseBook bookItem = mBooks.get(i);
					if(bookItem.mBookPath.equals(book.mBookPath)){
						Log.d(TAG, "path == "+bookItem.mBookPath);
						Toast.makeText(this, "该书已经添加", Toast.LENGTH_SHORT).show();
						return ;
					}
				}
				
				book.mBookId = (int)BookDao.getInstance().insertBook(this, book);
				mBookAdapter.addBooks(book);
	        	mBookAdapter.notifyDataSetChanged();
			}
		}
	}
    
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = new Intent();
		int v_id = v.getId();
		switch (v_id) {
		case R.id.findbook_btn:
			intent.putExtra("explorer_title","文件管理");
			intent.setDataAndType(Uri.fromFile(new File("/sdcard")), "*/*");
			intent.setClass(this, FilesActivity.class);
			startActivityForResult(intent, REQUEST_EX);
			break;
		case R.id.auto_findbook_btn:
			intent.putExtra("explorer_title","自动搜索");
			intent.setDataAndType(Uri.fromFile(new File("/sdcard")), "*/*");
			intent.setClass(this, AutoFilesActivity.class);
			startActivityForResult(intent, REQUEST_EX);
			break;
		case R.id.cam_scan_btn:
			intent =new Intent(this,CamTestActivity.class);
			startActivity(intent);
			break;
		case R.id.e_book_btn:
             
			intent =new Intent(this,EBookActivity.class);
			startActivity(intent);
			break;
			
		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		Intent intent = new Intent(this, ReaderActivity.class);
		BaseBook book = mBooks.get(position);
		intent.putExtra(ReaderActivity.BOOK_ID, book.mBookId);
		startActivity(intent);
	}
	
	
	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		// TODO Auto-generated method stub
		BaseBook book = mBooks.get(position);
		int reslut = BookDao.getInstance().deleteBook(this, book);
		int reslut1 = BookDao.getInstance().deleteBookMarksByBookId(this,book.mBookId);
		int reslut2 = BookDao.getInstance().deleteChapter(this, book.mBookId);
		
		Log.d(TAG, "reslut:"+reslut +"  reslut1:"+reslut1 + "  reslut2:" +reslut2);
		
		if(reslut >0){
			mBooks.removeElement(book);
			mBookAdapter.notifyDataSetChanged();
		}
		return false;
	}
}